package tech.yunland.shop.domain.dao;

import com.moyuer.bsf.domain.dao.BaseDao;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import tech.yunland.shop.domain.model.report.Income;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 王睿智<geekrize@gmail.com>
 */

@Repository
public class IncomeDao extends BaseDao<Income, String> {
	
    /**
     * 计入收入日报
     *
     * @param organId   机构标识
     * @param bizDate   业务日期
     * @param cash      现金
     * @param deduction 扣费
     * @param charge    充值
     */
    public void add(String organId, Date bizDate, BigDecimal cash, BigDecimal deduction, BigDecimal charge) {
        cash = cash == null ? BigDecimal.ZERO : cash;
        deduction = deduction == null ? BigDecimal.ZERO : deduction;
        charge = charge == null ? BigDecimal.ZERO : charge;

        DetachedCriteria criteria = DetachedCriteria.forClass(Income.class);
        criteria.add(Restrictions.eq("organId", organId));
        criteria.add(Restrictions.eq("bizDate", bizDate));
        List<Income> list = this.findByCriteria(criteria);

        Income income = null;
        if (list == null || list.size() == 0) {
            income = new Income();
            income.setOrganId(organId);
            income.setBizDate(bizDate);
            income.setCash(cash);
            income.setDeduction(deduction);
            income.setCharge(charge);
            this.save(income);
        }
        else if (list.size() == 1) {
            income = list.get(0);
            income.setCash(cash.add(income.getCash()));
            income.setDeduction(deduction.add(income.getDeduction()));
            income.setCharge(charge.add(income.getCharge()));
            this.update(income);
        }
        else {
            throw new RuntimeException("收入报表数据异常");
        }

    }

    /**
     * 将现金计入收入日报
     *
     * @param organId
     * @param bizDate
     * @param cash
     */
    public void addCash(String organId, Date bizDate, BigDecimal cash) {
        this.add(organId, bizDate, cash, null, null);
    }

    /**
     * 将扣费计入收入日报
     *
     * @param organId
     * @param bizDate
     * @param deduction
     */
    public void addDeduction(String organId, Date bizDate, BigDecimal deduction) {
        this.add(organId, bizDate, null, deduction, null);
    }

    /**
     * 将充值计入收入日报
     *
     * @param organId
     * @param bizDate
     * @param charge
     */
    public void addCharge(String organId, Date bizDate, BigDecimal charge) {
        this.add(organId, bizDate, null, null, charge);
    }
    
	/**
	 * 桌面端店铺日报(扣费/充值/现金)详情
	 * 
	 * @param type
	 * @param dateType
	 * @param bizDate
	 * @param organId
	 * 
	 * @return  List<Map<String, Object>> 
	 */
    public List<Map<String, Object>> getIncomeDesktopEntry(String type,String dateType, String bizDate, String organId,Integer page,Integer rows,String returnType){
    	
    	StringBuffer buffer=new StringBuffer();
    	
    	buffer.append("  select entry.id as id,entry.goodsName as goodsName,(case when  bu.name is null then '非会员' else bu.name end ) as memberName, ");
    	buffer.append("  entry.quantity as quantity,entry.amount as amount,entry.bizdate as bizdate,entry.organId as organId,entry.payment as payment ");
    	buffer.append("  from (select  bse.id as id,bs.bizdate as bizdate,g.name as goodsName,bs.member_id as memberId, ");
    	buffer.append("  bse.quantity as quantity,(case bse.payment when null then '无' when '10'   then 'deduction' when '40' then 'deduction'  when '20' then 'cash' else '无' end  ) as payment,bs.organ_id as organId,bse.amount as amount ");
    	buffer.append("  from bill_sale_entry bse left join bill_sale bs  on bs.id=bse.owner_id  left join goods g on g.id=bse.goods_id ");
    	buffer.append("  union ");
    	buffer.append("  select bce.id as id,bc.bizdate as bizdate,gp.name as goodsName,bc.member_id as memberId, ");
    	buffer.append("  bce.quantity as quantity,'charge' as payment,bc.organ_id as organId,bce.amount as amount ");
    	buffer.append("  from bill_charge_entry bce  left join bill_charge bc on bc.id=bce.owner_id  left join goods_package gp on bce.goodspackage_id=gp.id) entry ");
    	buffer.append(" left join bsf_users bu on bu.id=entry.memberId ");
    	
    	//取值跨度(时间)
		if (dateType.equals("year")) {
			buffer.append(" where to_char(entry.bizdate,'yyyy')='" + bizDate.substring(0,4) + "'  ");
		} else if (dateType.equals("month")) {
			buffer.append(" where to_char(entry.bizdate,'yyyy-MM')='" + bizDate.substring(0,7) + "'  ");
		} else if (dateType.equals("day")) {
			buffer.append(" where to_char(entry.bizdate,'yyyy-MM-dd')='" + bizDate.substring(0,10) + "'  ");
		}
    	
    	//取值范围(扣卡/现金/充值)
        if(type.equals("deduction")){
        	buffer.append("  and entry.payment='"+type+"'   ");
        }else if(type.equals("cash")){
        	buffer.append("  and entry.payment='"+type+"'   ");
        }else if(type.equals("charge")){
        	buffer.append("  and entry.payment='"+type+"'   ");
        }
        
        //组织隔离
        buffer.append(" and entry.organId ='"+organId+"' ");

        buffer.append(" order by bizdate desc");

        if(returnType.equals("size")){
        	return this.findBySQL(buffer.toString());
        }
		return this.findBySQL(buffer.toString(),page,rows);
    	
    }
}
